/*
 * Copyright 2006 Berry Crawford
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.judo.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil
{
	public static Date getDateFromStr(String s)
		throws Exception
	{
		Date date = null;
	
		date = getLongFormatDate(s);
		
		if (date == null)
			date = getShortFormatDate(s);
		
		if (date == null)
			date = getRegularLongFormatDate(s);
		
		if (date == null)
			date = getRegularFormatDate(s);
		
		if (date == null)
			date = getLongestFormatDate(s); 
		
		return date;
	}
	
	public static boolean isValidDate(String s)
	{
		Date date = null;
		
		date = getShortFormatDate(s);
		
		if (date == null)
			date = getLongFormatDate(s);
		
		if (date == null)
			date = getRegularLongFormatDate(s);
		
		if (date == null)
			date = getRegularFormatDate(s);
		
		if (date == null)
			return false;
		
		return true;
	}
	
	public static Date getShortFormatDate(String s)
	{
		Date date = null;
	
		try
		{
			SimpleDateFormat shortDate = new SimpleDateFormat("yyyy-MM-dd");
			//shortDate.setLenient(true);
			
			date = shortDate.parse(s);
		}
		catch (Exception e)
		{
			
		}
		
		return date;
	}
	
	public static Date getRegularFormatDate(String s)
	{
		Date date = null;
	
		try
		{
			SimpleDateFormat shortDate = new SimpleDateFormat("MM/dd/yyyy");
			//shortDate.setLenient(true);
			
			date = shortDate.parse(s);
		}
		catch (Exception e)
		{
			
		}
		
		return date;
	}
	
	public static Date getLongestFormatDate(String s)
	{
		Date date = null;
	
		try
		{
			SimpleDateFormat longestDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
			//shortDate.setLenient(true);
			
			date = longestDate.parse(s);
		}
		catch (Exception e)
		{
			
		}
		
		return date;
	}
	
	public static Date getRegularLongFormatDate(String s)
	{
		Date date = null;
	
		try
		{
			SimpleDateFormat shortDate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
			//shortDate.setLenient(true);
			
			date = shortDate.parse(s);
		}
		catch (Exception e)
		{
			
		}
		
		return date;
	}
	
	public static Date getLongFormatDate(String s)
	{
		Date date = null;
	
		try
		{
			SimpleDateFormat longDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//longDate.setLenient(true);
			
			date = longDate.parse(s);
		}
		catch (Exception e)
		{
			
		}
		
		return date;
	}
	
	public static String getStringValue(Date date,String pattern)
	{
		String result = null;
		
		try
		{
			SimpleDateFormat longDate = new SimpleDateFormat(pattern);
			//longDate.setLenient(true);
			result = longDate.format(date);
		}
		catch(Exception e)
		{
			
		}
		
		return result;
	}
}
